<?php

if(!defined('_LIB_SAVE_')) {
define('_LIB_BASE_', 1);

include _FLEP_LIB_DIR_.'common.inc.php';

define('_FLEP_INSERT_', 1);
define('_FLEP_UPDATE_', 2);
define('_FLEP_DELETE_', 3);

function flep_save(&$data) {
  foreach($data['data'] as $idx => &$struct){
    if ($struct['id'] === "") {
      $query[$idx] = flep_prepare_insert($data, $idx);
    } else {
      $query[$idx] = flep_prepare_update($data, $idx);
    }
  }
  return flep_query($data, $query);
}

function flep_prepare_insert(&$data, $idx) {
  $data['action'][$idx] = _FLEP_INSERT_;
  foreach ($data['data'][$idx] as $key => $value) {
    if ($key !== "id") {
      isset($vals) ? $vals .= ',' : $vals = '';
      $vals .= '\'' . flep_db_insert_escape($value) . '\'';
    }
  }
  $cols = '`' . implode('`,`', array_keys($data['data'][$idx])) . '`';
  return "INSERT INTO " . $data['table'] . " ($cols) VALUES (null,$vals)";
}

function flep_prepare_update(&$data, $idx) {
  $data['action'][$idx] = _FLEP_UPDATE_;
  $data['data'][$idx]['id'] = flep_decode($data['data'][$idx]['id']);
  foreach ($data['data'][$idx] as $key => $value) {
    isset($val) ? $val .= ',' : $val = '';
    $val .= '`' . $key . '`=\'' . flep_db_insert_escape($value) . '\'';
  }
  return "UPDATE " . $data['table'] . " SET $val WHERE id=" . $data['data'][$idx]['id'];
}

function flep_query(&$data, $query) {
  $db = flep_db_connect();
  if (!flep_db_error($db)) {
    foreach($query as $idx => &$qry) {
      if (!flep_db_query($db, $qry)) {
        $data['msg'] = flep_db_error($db);
        $data['errno'] = $db['errno'];
        return false;
      }
      if ($data['action'][$idx] === _FLEP_INSERT_) {
        $data['data'][$idx]['id'] = flep_db_last_id($db);
      }
    }
  } else {
    $data['msg'] = flep_db_error($db);
    $data['errno'] = $db['errno'];
    return false;
  }
  flep_db_close($db);
  return true;
}

function flep_delete_internal(&$data) {
  $data['action'][0] = _FLEP_DELETE_;
  $query[0] = "DELETE FROM " . $data['table'] . " WHERE id='" . $data['data'][0]['id'] . "'";
  return flep_query($data, $query);
}

function flep_delete(&$data) {
  $data['data'][0]['id'] = flep_decode($data['data'][0]['id']);
  return flep_delete_internal($data);
}

function flep_decode_list_id(&$data) {
  if(array_key_exists('list_fields', $data)){
    foreach ($data['list_fields'] as $key => &$field) {
      foreach($data['data'] as &$item) {
        $item[$key] = flep_decode($item[$key]);
      }
    }
  }
}

function flep_auto_code(&$data) {
    $y = strtotime($data['data'][0]['fecha_ingreso']);
    $p = preg_split('/\s+/', $data['data'][0]['apellidos']);
    $t = count($p);
    if ($t > 1) {
        $in = substr($p[0], 0, 1) . substr($p[1], 0, 1);
    } else {
        $in = substr($p[0], 0, 1);
        $in .= $in;
    }
    $in = strtoupper($in) . date("y", $y);
    $rtn = 1;
    flep_structure($structure, $data['type']);
    if (flep_search($structure, sprintf("SELECT `usuario` FROM `%s` WHERE `usuario` LIKE '%s%%' ORDER BY `usuario` DESC",$data['table'],$in))) {
        $s = $structure['data'][0]['usuario'];
        $rtn += intval(substr($s, 4));
    }
    return $in . sprintf("%04d", $rtn);
}

}
?>
